package com.sisSaude.daos;

import java.sql.ResultSet;
import java.util.Vector;

import com.sisSaude.entityBeans.ItemDeFichaDeAnamnese;
import com.sisSaude.utils.DataProvider;
import com.sisSaude.utils.SqlFactory;

public class ItemDeFichaDeAnamneseDAO extends GenericDAO<ItemDeFichaDeAnamnese> {

	// fields
	public static String SELECT_BASICA = " SELECT ItemDeFichaDeAnamnese.*," +
			                             "        PerguntaDeFichaDeAnamnese.descricao AS descricaoDaPergunta " +
			                             " FROM ItemDeFichaDeAnamnese " + 
			                             " INNER JOIN PerguntaDeFichaDeAnamnese ON PerguntaDeFichaDeAnamnese.id = ItemDeFichaDeAnamnese.idPergunta ";
	
	// public methods
	public boolean delete(ItemDeFichaDeAnamnese item) {
		String sql = SqlFactory.getSqlDelete("ItemDeFichaDeAnamnese", "id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(item.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}
	public ItemDeFichaDeAnamnese get(int id) {
		ItemDeFichaDeAnamnese item = null;
		String sql = SELECT_BASICA + " WHERE ItemDeFichaDeAnamnese.id = ? "; 
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(id);
		try {
			dataProvider.select(sql, params);
			if (dataProvider.next()) {
				item = new ItemDeFichaDeAnamnese();
				loadAttribs(item, dataProvider.getResultSet());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			dataProvider.close();
		}
		return item;
	}
	public void loadAttribs(ItemDeFichaDeAnamnese item, ResultSet r) {
		try {
			item.setId(r.getInt("id"));
			item.setIdFicha(r.getInt("idFicha"));
			item.setIdPergunta(r.getInt("idPergunta"));
			item.setResposta(r.getString("resposta"));
			item.setDescricaoDaPergunta(r.getString("descricaoDaPergunta"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// protected methods
	protected boolean insert(ItemDeFichaDeAnamnese item) {
		String sql = SqlFactory.getSqlInsert("ItemDeFichaDeAnamnese");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(item.getId());
		params.add(item.getIdFicha());
	 	params.add(item.getIdPergunta());
	 	params.add(item.getResposta());
	 	try {
			item.setId(dataProvider.insert(sql, params));
			return item.getId() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
	}
	protected boolean update(ItemDeFichaDeAnamnese item) {
		String sql = SqlFactory.getSqlUpdate("ItemDeFichaDeAnamnese", "ItemDeFichaDeAnamnese.id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(item.getIdFicha());
	 	params.add(item.getIdPergunta());
	 	params.add(item.getResposta());
		params.add(item.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}
}
